package com.qqla.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qqla.context.BaseContext;
import com.qqla.dto.LoginDto;
import com.qqla.entity.User;
import com.qqla.enumeration.AppHttpCodeEnum;
import com.qqla.mapper.LoginMapper;
import com.qqla.result.ResponseResult;
import com.qqla.service.LoginService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, User> implements LoginService {

    /*
    * 登录方法
    * */
    @Override
    public ResponseResult login(LoginDto dto) {
        // 查询sql是否存在用户
        User user = lambdaQuery().eq(User::getUsername, dto.getUsername()).one();

        // 如果为空 返回错误信息
        if(user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USERNAME_ERROR);
        }

        // 查询到信息对比密码信息
        if(!(DigestUtils.md5DigestAsHex(dto.getPassword().getBytes()).equals(user.getPassword()))) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PASSWORD_ERROR);
        }

        // 修改密码为**
        user.setPassword("******");
        return ResponseResult.okResult(user);
    }
}
